<?php

namespace app\modules\user\controllers;

use app\common\controllers\AccessController;
use app\modules\user\forms\order\OrderForm;
use app\modules\user\services\GoodsOrderService;
use yii\base\UserException;

/**
 * 商品订单
 * Class GoodsOrderController
 * @package app\modules\user\controllers
 */
class GoodsOrderController extends AccessController{

    //搜索
    public function actionSearch(){
        $merchant_name = \Yii::$app->getRequest()->get("merchant_name");
        $pagesize = \Yii::$app->getRequest()->get("pagesize");

        return [
            'list'=>GoodsOrderService::search(\Yii::$app->getModule("user")->get("user")->getId(),$merchant_name,$page,$pagesize),
            'page'=>$page->Out(),
        ];
    }

    //获取信息
    public function actionGetInfoById($id){
        return GoodsOrderService::getInfoById(\Yii::$app->getModule("user")->get("user")->getId(),$id);
    }

    //添加
    public function actionAdd(){
        $form = new OrderForm();
        if($form->load(\Yii::$app->getRequest()->post(),'') && ($result = GoodsOrderService::create($form))){
            return $result;
        }else{
            throw new UserException(GoodsOrderService::getError());
        }
    }

    //取消
    public function actionCancel($id){
        if(GoodsOrderService::cancel(\Yii::$app->getModule("user")->get("user")->getId(),$id)){
            return "";
        }else{
            throw new UserException(GoodsOrderService::getError());
        }
    }
}